﻿using System.Runtime.InteropServices;

namespace TcpServer
{
    internal class Program
    {
        static void TcpServerTest()
        {
            // 创建并启动一个简单的TCP服务器
            var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 10086);
            listener.Start();
            Console.WriteLine("TCP Server is running on port 10086...");
            listener.AcceptTcpClientAsync().ContinueWith(t =>
            {
                var client = t.Result;
                Console.WriteLine("Client connected.");
                var stream = client.GetStream();
                var buffer = new byte[1024];
                stream.ReadAsync(buffer, 0, buffer.Length).ContinueWith(readTask =>
                {
                    var bytesRead = readTask.Result;
                    var message = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead);
                    Console.WriteLine($"Received: {message}");
                    client.Close();
                    listener.Stop();
                });
            });
        }
        [DllImport("*")]
        internal static extern void SetIcsNativeaotActiveCode(byte[] activeCode);
        static void Main(string[] args)
        {
            var activeCode = new byte[] { 0x3B, 0xD9, 0xDD, 0xF5, 0x00, 0xCE, 0x42, 0x8D, 0x5D, 0xAA, 0xEA, 0x90, 0x81, 0xB8, 0xEE, 0x80, 0xAB, 0xCD, 0x78, 0x71, 0xA3, 0xB1, 0x97, 0x4F, 0x9C, 0x01, 0xCE, 0xE2, 0x58, 0x0A, 0x84, 0xB1, };
            //获取当前platform
            var platform = RuntimeInformation.ProcessArchitecture;
            if (platform == Architecture.Arm)
            {
                SetIcsNativeaotActiveCode(activeCode);
            }
            TcpServerTest();
            Console.WriteLine("Test Complete!");
        }
    }
}
